<?php

class OrdercustomerController extends BaseController {
    /*
      |--------------------------------------------------------------------------
      | Default Home Controller
      |--------------------------------------------------------------------------
      |
      | You may wish to use controllers instead of, or in addition to, Closure
      | based routes. That's great! Here is an example controller method to
      | get you started. To route to this controller, just add the route:
      |
      |	Route::get('/', 'HomeController@showWelcome');
      |
     */

    public function getIndex() {
        return View::make('index');
    }

    public function getAgenreg() {
        $agen = DB::table('sys_agen')
                ->join('sys_member', 'member_agen_id', '=', 'agen_id')
                ->where('agen_id', '<>', '0')
                ->where('member_status', 'Aktif')
                ->get();

        $provinsi = provinsi::all();
        return View::make('agenreg', compact('agen', $agen, 'provinsi', $provinsi));
    }

    public function getCekorder() {
        return View::make('cekorder');
    }

    public function postCekorder() {
        $order = DB::table('sys_order')
                ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                ->leftjoin('sys_invoice', 'invoice_order_id', '=', 'order_id')
                ->leftjoin('sys_invoice_detail', 'invoice_detail_invoice_id', '=', 'invoice_id')
                ->where('order_code', '=', Input::get('order_code'))
                ->first();
        $oc = DB::table('sys_order')
                ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                ->leftjoin('sys_invoice', 'invoice_order_id', '=', 'order_id')
                ->where('order_code', '=', Input::get('order_code'))
                ->count();
        $total = DB::table('sys_order')
                ->leftjoin('sys_order_progress', 'order_progress_order_id', '=', 'order_id')
                ->where('order_code', Input::get('order_code'))
                ->first();

        $progress = DB::table('sys_order')
                ->leftjoin('sys_order_progress', 'order_progress_order_id', '=', 'order_id')
                ->where('order_code', Input::get('order_code'))
                ->orderBy('order_id')
                ->groupBy('order_id')
                ->first();

        return View::make('status', compact('order', $order, 'oc', $oc, 'total', $total, 'progress', $progress));
    }

    public function postAgenreg() {
        $rules = array(
            'member_email' => 'required|unique:sys_member,member_email',
        );
        $notif = array(
            'required' => ' <font color="red"> Kolom harus diisi.</font><br>',
            'unique' => ' <font color="red"> Email sudah dipakai.</font><br>',);

        $validator = Validator::make(Input::all(), $rules, $notif);

        if ($validator->fails()) {
            return Redirect::back()->with('error', 'Email sudah dipakai, silahkan menggunakan email lain.');
        }

        $agen = New agen();
        $agen->agen_admin_group_id = 2;
        $agen->agen_sponsor_id = Input::get('id_agen');
        $agen->save();

        $agencode = agen::find($agen->agen_id);
        $hasil = $agen->agen_id + 1;
        $pk_n = strlen($hasil);
        if ($pk_n == 1) {
            $noagen = "000" . $hasil;
        }
        if ($pk_n == 2) {
            $noagen = "00" . $hasil;
        }
        if ($pk_n == 3) {
            $noagen = "0" . $hasil;
        }
        if ($pk_n == 4) {
            $noagen = $hasil;
        }
        $agencode->agen_code = 'AGEN' . $noagen;
        $agencode->save();

        $detailagen = New member();
        $detailagen->member_agen_id = $agen->agen_id;
        $reg_id = regional::where('regional_name', Input::get('member_regional_name'))->first();
        $detailagen->member_regional_id = $reg_id->regional_id;
        $detailagen->member_provinsi_id = Input::get('member_provinsi_id');
        $detailagen->member_name = Input::get('member_name');
        $detailagen->member_phone = Input::get('member_phone');
        $detailagen->member_email = Input::get('member_email');
        $detailagen->member_address = Input::get('member_address');
        $detailagen->member_joindate = date('Y-m-d H:i:s');
        $detailagen->member_status = 'Tidak aktif';
        $detailagen->save();


        $bonus = refbonus::where('bonus_config_name', 'referal')->first();

        $bonuslog = New bonuslog();
        $bonuslog->bonus_log_agen_id = Input::get('id_agen');
        $bonuslog->bonus_log_referal = $bonus->bonus_config_percentage;
        $bonuslog->bonus_log_date = date('Y-m-d H:i:s');
        $bonuslog->save();

        $bonus = New bonus();
        $bonus->bonus_agen_id = $agen->agen_id;
        $bonus->bonus_referal = 0;
        $bonus->bonus_order = 0;
        $bonus->save();

        $sumbonus = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', Input::get('id_agen'))
                ->sum('bonus_log_referal');

        $rebonus = bonus::find(Input::get('id_agen'));
        $rebonus->bonus_referal = $sumbonus;
        $rebonus->save();

        Mail::send('emails.terimakasihagen', array('pesan' => 'Terima kasih atas pendaftaran anda.'), function($message) {
            $email = Input::get('member_email');
            $nama = Input::get('member_name');
            $message->to($email, $nama)->subject('Pendaftaran sukses');
        });

        return Redirect::to('/')->with('message', 'Pendaftaran sukses');
    }

    //order id card
    public function getOrderbrosur() {
        Session::put('ordertype', 'Brosur');
        $jenis = DB::table('ref_brochure')
                ->where('brochure_isdelete', '0')
                ->get();

        $brosurkertas = DB::table('ref_papertype')
                ->where('papertype_type', '=', 'brochure')
                ->where('papertype_isdelete', '0')
                ->get();
        $envelopekertas = DB::table('ref_papertype')
                ->where('papertype_type', '=', 'envelope')
                ->where('papertype_isdelete', '0')
                ->get();
        $flayerkertas = DB::table('ref_papertype')
                ->where('papertype_type', '=', 'flayer')
                ->where('papertype_isdelete', '0')
                ->get();
        $letterheadkertas = DB::table('ref_papertype')
                ->where('papertype_type', '=', 'letterhead')
                ->where('papertype_isdelete', '0')
                ->get();

        $brosursize = DB::table('ref_size_brochure')
                ->where('size_brochure_isdelete', '0')
                ->get();
        $envelopesize = DB::table('ref_size_envelope')
                ->where('size_envelope_isdelete', '0')
                ->get();
        $flayersize = DB::table('ref_size_flayer')
                ->where('size_flayer_isdelete', '0')
                ->get();
        $letterheadsize = DB::table('ref_size_letterhead')
                ->where('size_letterhead_isdelete', '0')
                ->get();
        return View::make('order.orderbrosur', compact('jenis', $jenis, 'brosurkertas', $brosurkertas, 'envelopekertas', $envelopekertas, 'letterheadkertas', $letterheadkertas, 'flayerkertas', $flayerkertas, 'brosursize', $brosursize, 'envelopesize', $envelopesize, 'letterheadsize', $letterheadsize, 'flayersize', $flayersize));
    }

    //order id card
    public function getOrderidcard() {
        Session::put('ordertype', 'ID Card');
        $jenis = DB::table('ref_idcard_type')
                ->where('idcard_type_isdelete', '0')
                ->get();
        $kualitas = DB::table('ref_idcard_quality')
                ->where('idcard_quality_isdelete', '0')
                ->get();
        return View::make('order.orderidcard', compact('jenis', $jenis, 'kualitas', $kualitas));
    }

    //order packaging
    public function getOrderpackaging() {
        Session::put('ordertype', 'Packaging');
        $jenis = DB::table('ref_packaging')
                ->where('packaging_isdelete', '0')
                ->groupBy('packaging_type')
                ->get();
        $ukuran = DB::table('ref_packaging')
                ->where('packaging_isdelete', '0')
                ->groupBy('packaging_width')
                ->get();
        $kertas = DB::table('ref_papertype')
                ->where('papertype_type', '=', 'packaging')
                ->where('papertype_isdelete', '0')
                ->get();
        $jumlah = DB::table('ref_packaging')
                ->where('packaging_isdelete', '0')
                ->groupBy('packaging_qty_price')
                ->get();

        return View::make('order.orderpackaging', compact('jenis', $jenis, 'ukuran', $ukuran, 'jumlah', $jumlah, 'kertas', $kertas));
    }

    //order tshirt
    public function getOrdertshirt() {
        Session::put('ordertype', 'Kaos');
        $jenis = DB::table('ref_tshirt')
                ->where('tshirt_isdelete', '0')
                ->groupBy('tshirt_type_of_material')
                ->get();


        return View::make('order.ordertshirt')->with('jenis', $jenis);
    }

    //order nota
    public function getOrdernota() {
        Session::put('ordertype', 'Nota');
        $jenisnota = DB::table('ref_bill')
                ->where('bill_isdelete', '0')
                ->groupBy('bill_name')
                ->get();

        $kertas = DB::table('ref_papertype')
                ->where('papertype_isdelete', '0')
                ->where('papertype_type', '=', 'bill')
                ->get();


        return View::make('order.ordernota', compact('jenisnota', $jenisnota, 'kertas', $kertas));
    }

    public function getOrderback() {
        if (Session::get('ordertype') == 'ID Card') {
            return Redirect::to('order/orderidcard');
        } else if (Session::get('ordertype') == 'Packaging') {
            return Redirect::to('order/orderpackaging');
        } else if (Session::get('ordertype') == 'Brosur') {
            return Redirect::to('order/orderbrosur');
        } else if (Session::get('ordertype') == 'Kaos') {
            return Redirect::to('order/ordertshirt');
        } else if (Session::get('ordertype') == 'Nota') {
            return Redirect::to('order/ordernota');
        }
    }

    public function getOrderbackend() {
        if (Session::get('ordertype') == 'ID Card') {
            return Redirect::to('order/fiturtambahan');
        } else if (Session::get('ordertype') == 'Packaging') {
            return Redirect::to('order/fiturtambahan');
        } else if (Session::get('ordertype') == 'Brosur') {
            return Redirect::to('order/orderbrosur');
        } else if (Session::get('ordertype') == 'Kaos') {
            return Redirect::to('order/fiturtambahan');
        } else if (Session::get('ordertype') == 'Nota') {
            return Redirect::to('order/ordernota');
        }
    }

    public function postOrderpackaging() {
        if (Input::get('pilihan_ukuran') == 1) {
            $rules = array(
                'length' => 'required',
                'width' => 'required',
                'height' => 'required',
            );
            $notif = array(
                'required' => ' <font color="red">  Kolom harus diisi</font>',
            );

            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->with('errorsize', '<font color="red">  Kolom harus diisi</font>')
                                ->withInput();
            }
            Session::put('length', Input::get('length'));
            Session::put('width', Input::get('width'));
            Session::put('height', Input::get('height'));
            Session::put('jenis_ukuran', Input::get('length') . ' x ' . Input::get('width') . ' x ' . Input::get('height'));
        } else {
            $rules = array(
                'ukuran_standar' => 'required',
            );
            $notif = array(
                'required' => ' <font color="red">  Kolom harus diisi</font>',
            );

            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->withErrors($validator)
                                ->withInput();
            }
            Session::put('ukuran_standar', Input::get('ukuran_standar'));
            $size = DB::table('ref_packaging')
                    ->where('packaging_id', Input::get('ukuran_standar'))
                    ->first();
            Session::put('length', $size->packaging_length);
            Session::put('width', $size->packaging_width);
            Session::put('height', $size->packaging_height);
            Session::put('jenis_ukuran', $size->packaging_length . ' x ' . $size->packaging_width . ' x ' . $size->packaging_height);
        }

        if (Input::get('pilihan_jumlah') == 1) {
            $rules = array(
                'jumlah_custom' => 'required',
            );
            $notif = array(
                'required' => ' <font color="red">  Kolom harus diisi</font>',
            );
            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->withErrors($validator)
                                ->withInput();
            }
            Session::put('qty', Input::get('jumlah_custom'));
            Session::put('jumlah_custom', Input::get('jumlah_custom'));
        } else {
            $rules = array(
                'jumlah_standar' => 'required',
            );
            $notif = array(
                'required' => ' <font color="red">  Kolom harus diisi</font>',
            );
            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->withErrors($validator)
                                ->withInput();
            }
            Session::put('qty', Input::get('jumlah_standar'));
            Session::put('jumlah_standar', Input::get('jumlah_standar'));
        }

        $rules = array(
            'jenis_packaging' => 'required',
            'jenis_kertas' => 'required',
            'pilihan_ukuran' => 'required',
        );
        $notif = array(
            'required' => ' <font color="red">  Kolom harus diisi</font>'
        );

        $validator = Validator::make(Input::all(), $rules, $notif);
        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator)
                            ->withInput();
        }


        Session::put('jenis_packaging', Input::get('jenis_packaging'));
        Session::put('jenis_kertas', Input::get('jenis_kertas'));
        Session::put('pilihan_jumlah', Input::get('pilihan_jumlah'));
        Session::put('pilihan_ukuran', Input::get('pilihan_ukuran'));

        if (Input::hasFile('design_packaging')) {
            // ambil file
            $picture = Input::file('design_packaging');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/packaging', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order_packaging_design = '/design/packaging/' . $picture->getClientOriginalName();
            Session::put('design', $order_packaging_design);
        }

        $packagingcount = DB::table('ref_packaging')
                ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                ->where('packaging_type', Input::get('jenis_packaging'))
                ->where('packaging_length', Session::get('length'))
                ->where('packaging_width', Session::get('width'))
                ->where('packaging_height', Session::get('height'))
                ->where('packaging_qty_price', Session::get('qty'))
                ->where('papertype_name', Input::get('jenis_kertas'))
                ->count();
        $packaging = DB::table('ref_packaging')
                ->join('ref_papertype', 'papertype_id', '=', 'packaging_papertype_id')
                ->where('packaging_type', Input::get('jenis_packaging'))
                ->where('packaging_length', Session::get('length'))
                ->where('packaging_width', Session::get('width'))
                ->where('packaging_height', Session::get('height'))
                ->where('packaging_qty_price', Session::get('qty'))
                ->where('papertype_name', Input::get('jenis_kertas'))
                ->first();
        if ($packagingcount == 0) {
            $packaging_price = 0;
            $packaging_id = 0;
        } else {
            $packaging_id = $packaging->packaging_id;
            $packaging_price = $packaging->packaging_price;
        }
        Session::put('packaging_price', $packaging_price);
        Session::put('packaging_id', $packaging_id);

        return Redirect::to('order/fiturtambahan');
    }

    public function postOrderidcard() {
        if (Input::get('pilihan_jumlah') == '1') {
            $rules = array(
                'jenis_idcard' => 'required',
                'kualitas_idcard' => 'required',
                'pilihan_jumlah' => 'required',
                'jumlah_idcard' => 'required|numeric|min:11',
            );
            $notif = array(
                'required' => ' <font color="red">  Kolom harus diisi</font>',
                'numeric' => ' <font color="red">  Harap diisi dengan angka</font>',
                'min' => ' <font color="red">  Silahkan order lebih dari 10</font>',
            );


			$validator = Validator::make(Input::all(), $rules, $notif);
			if($validator->fails()){
				echo "asu";
				return Redirect::back()->withErrors($validator)
										->withInput();
            }
            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->withErrors($validator)
                                ->withInput();
            }
            if (Input::hasFile('design_idcard')) {
                // ambil file
                $picture = Input::file('design_idcard');
                // simpan file ke directory upload yang terletak di folder public
                $picture->move(public_path() . '/design/idcard', $picture->getClientOriginalName());
                // simpan path ke file foto di database
                $order_idcard_design = '/design/idcard/' . $picture->getClientOriginalName();
                Session::put('design', $order_idcard_design);
            }
            // $order->save();
            Session::put('pilihan_jumlah', Input::get('pilihan_jumlah'));
            Session::put('jumlah_idcard', Input::get('jumlah_idcard'));
            Session::put('jenis_idcard', Input::get('jenis_idcard'));
            Session::put('kualitas_idcard', Input::get('kualitas_idcard'));

		
		return Redirect::to('order/fiturtambahan');						
		}else{
		$rules = array(
				'jenis_idcard' => 'required',
				'kualitas_idcard' => 'required',
				'pilihan_jumlah' => 'required',
			);	
		$notif = array(
				'required' => ' <font color="red">  Kolom harus diisi</font>'
				);

            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->withErrors($validator)
                                ->withInput();
            }

            // $order = new orderidcard;
            // $order->order_idcard_type = Input::get('jenis_idcard');
            // $order->order_idcard_quality = Input::get('kualitas_idcard');
            if (Input::hasFile('design_idcard')) {
                // ambil file
                $picture = Input::file('design_idcard');
                // simpan file ke directory upload yang terletak di folder public
                $picture->move(public_path() . '/design/idcard', $picture->getClientOriginalName());
                // simpan path ke file foto di database
                $order_idcard_design = '/design/idcard/' . $picture->getClientOriginalName();
                Session::put('design', $order_idcard_design);
            }
            // $order->save();
            Session::put('pilihan_jumlah', Input::get('pilihan_jumlah'));
            Session::put('jumlah_idcard', Input::get('jumlah_idcard'));
            Session::put('jenis_idcard', Input::get('jenis_idcard'));
            Session::put('kualitas_idcard', Input::get('kualitas_idcard'));


            return Redirect::to('order/fiturtambahan');
        }
    }

    public function postOrdertshirt() {

        if (Input::get('pilihan_jumlah') == 1) {
            $rules = array(
                'jumlah_custom' => 'required',
            );
            $notif = array(
                'required' => ' <font color="red">  Kolom harus diisi</font>',
            );
            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->withErrors($validator)
                                ->withInput();
            }
            Session::put('qty', Input::get('jumlah_custom'));
            Session::put('jumlah_custom', Input::get('jumlah_custom'));
        } else {
            $rules = array(
                'jumlah_standar' => 'required',
            );
            $notif = array(
                'required' => ' <font color="red">  Kolom harus diisi</font>',
            );
            $validator = Validator::make(Input::all(), $rules, $notif);
            if ($validator->fails()) {
                return Redirect::back()->withErrors($validator)
                                ->withInput();
            }
            Session::put('qty', Input::get('jumlah_standar'));
            Session::put('jumlah_standar', Input::get('jumlah_standar'));
        }

        $rules = array(
            'jenis_bahan_kaos' => 'required',
            'pilihan_jumlah' => 'required',
        );
        $notif = array(
            'required' => ' <font color="red">  Kolom harus diisi</font>'
        );

        $validator = Validator::make(Input::all(), $rules, $notif);
        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator)
                            ->withInput();
        }


        Session::put('jenis_bahan_kaos', Input::get('jenis_bahan_kaos'));

        if (Input::hasFile('design_kaos')) {
            // ambil file
            $picture = Input::file('design_kaos');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/tshirt', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order_kaos_design = '/design/tshirt/' . $picture->getClientOriginalName();
            Session::put('design', $order_kaos_design);
        }

        $tshirtcount = DB::table('ref_tshirt_detail')
                ->where('tshirt_detail_tshirt_id', Session::get('jenis_bahan_kaos'))
                ->where('tshirt_detail_qty_price', Session::get('qty'))
                ->count();
        $tshirt = DB::table('ref_tshirt_detail')
                ->where('tshirt_detail_tshirt_id', Session::get('jenis_bahan_kaos'))
                ->where('tshirt_detail_qty_price', Session::get('qty'))
                ->first();
        if ($tshirtcount == 0) {
            $tshirt_price = 0;
            $tshirtcount = DB::table('ref_tshirt_detail')
                    ->where('tshirt_detail_tshirt_id', Session::get('jenis_bahan_kaos'))
                    ->first();
            $tshirt_detail_id = $tshirt->tshirt_detail_id;
        } else {
            $tshirt_detail_id = $tshirt->tshirt_detail_id;
            $tshirt_price = $tshirt->tshirt_detail_price;
        }
        Session::put('tshirt_price', $tshirt_price);
        Session::put('tshirt_detail_id', $tshirt_detail_id);

        return Redirect::to('order/fiturtambahan');
    }

    public function postOrdernota() {



        $rules = array(
            'jenis_nota' => 'required',
            'jenis_kertas' => 'required',
            'jumlah_warna' => 'required',
            'jumlah_order' => 'required',
        );
        $notif = array(
            'required' => ' <font color="red">  Kolom harus diisi</font>'
        );

        $validator = Validator::make(Input::all(), $rules, $notif);
        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator)
                            ->withInput();
        }


        Session::put('jenis_nota', Input::get('jenis_nota'));
        Session::put('jenis_kertas', Input::get('jenis_kertas'));
        Session::put('jumlah_warna', Input::get('jumlah_warna'));
        Session::put('qty', Input::get('jumlah_order'));

        if (Input::hasFile('design_nota')) {
            // ambil file
            $picture = Input::file('design_nota');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/nota', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order_nota_design = '/design/nota/' . $picture->getClientOriginalName();
            Session::put('design', $order_nota_design);
        }

        $billcount = DB::table('ref_bill')
                ->join('ref_papertype', 'papertype_id', '=', 'bill_papertype_id')
                ->where('bill_id', Input::get('jenis_nota'))
                ->where('bill_qty_color', Session::get('jumlah_warna'))
                ->where('bill_papertype_id', Input::get('jenis_kertas'))
                ->count();
        $bill = DB::table('ref_bill')
                ->join('ref_papertype', 'papertype_id', '=', 'bill_papertype_id')
                ->where('bill_id', Input::get('jenis_nota'))
                ->where('bill_qty_color', Session::get('jumlah_warna'))
                ->where('bill_papertype_id', Input::get('jenis_kertas'))
                ->first();

        if ($billcount == 0) {
            $bill_price = 0;
            $bill_id = Input::get('jenis_nota');
        } else {
            $bill_id = $bill->bill_id;
            $bill_price = $bill->bill_price;
        }
        Session::put('bill_price', $bill_price);
        Session::put('bill_id', $bill_id);

        return Redirect::to('order/fiturtambahan');
    }

    public function postOrderbrosur() {



        $rules = array(
            'jenis_cetak' => 'required',
            'brosur_size' => 'required',
            'jenis_kertas' => 'required',
            'warna' => 'required',
            'qty_print_page' => 'required',
            'jumlah_order' => 'required',
        );
        $notif = array(
            'required' => ' <font color="red">  Kolom harus diisi</font>'
        );

        $validator = Validator::make(Input::all(), $rules, $notif);
        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator)
                            ->withInput();
        }


        Session::put('jenis_cetak', Input::get('jenis_cetak'));
        Session::put('jenis_kertas', Input::get('jenis_kertas'));
        Session::put('brosur_size', Input::get('brosur_size'));
        Session::put('warna', Input::get('warna'));
        Session::put('qty_print_page', Input::get('qty_print_page'));
        Session::put('qty', Input::get('jumlah_order'));
        Session::put('brosur_unit', Input::get('brochure_unit'));

        if (Input::hasFile('design_brosur')) {
            // ambil file
            $picture = Input::file('design_brosur');
            // simpan file ke directory upload yang terletak di folder public
            $picture->move(public_path() . '/design/brosur', $picture->getClientOriginalName());
            // simpan path ke file foto di database
            $order_brosur_design = '/design/brosur/' . $picture->getClientOriginalName();
            Session::put('design', $order_brosur_design);
        }

        $brosurcount = DB::table('ref_brochure')
                ->where('brochure_type', Input::get('jenis_cetak'))
                ->where('brochure_qty_print_page', Session::get('qty_print_page'))
                ->where('brochure_papertype_id', Input::get('jenis_kertas'))
                ->where('brochure_color', Input::get('warna'))
                ->count();
        $brosur = DB::table('ref_brochure')
                ->where('brochure_type', Input::get('jenis_cetak'))
                ->where('brochure_qty_print_page', Session::get('qty_print_page'))
                ->where('brochure_papertype_id', Input::get('jenis_kertas'))
                ->where('brochure_color', Input::get('warna'))
                ->first();
        if ($brosurcount == 0) {
            $brosur_price = 0;
            $brosur_id = 0;
        } else {
            $brosur_id = $brosur->brochure_id;
            $brosur_price = $brosur->brochure_price;
        }
        Session::put('brosur_price', $brosur_price);
        Session::put('brosur_id', $brosur_id);
        // echo $brosur_price;

        return Redirect::to('order/fiturtambahan');
    }

    //fitur tambahan
    public function GetFiturtambahan() {
        $ref = DB::table('ref_additional_features')
                ->where('additional_features_type', Session::get('ordertype'))
                ->get();

        if ((Session::get('ordertype') == 'Brosur') OR (Session::get('ordertype') == 'Nota')) {
            return Redirect::to('order/datapemesan');
        } else {

            return View::make('order.fiturtambahan')->with('ref', $ref);
        }
    }

    public function postFiturtambahan() {
        $ref = DB::table('ref_additional_features')
                ->get();
        foreach ($ref as $ref) {
            Session::put($ref->additional_features_name, Input::get($ref->additional_features_id));
        }

        return Redirect::to('order/datapemesan');
    }

    public function GetDatapemesan() {
        $price_quality = DB::table('ref_idcard_quality')
                ->where('idcard_quality_id', Session::get('kualitas_idcard'))
                ->first();

        $ref = DB::table('ref_additional_features')
                ->get();
        $price_add_features = 0;
        foreach ($ref as $ref) {
            if (Session::get($ref->additional_features_name) == '1') {
                $price_add_features = $price_add_features + $ref->additional_features_price;
            }
        }


        if (Session::get('ordertype') == "ID Card") {
            if (Session::get('pilihan_jumlah') == "0") {
                $jumlah = 10;
            } else {
                $jumlah = Session::get('jumlah_idcard');
            }
            $total_price = ($price_quality->idcard_quality_price + $price_add_features) * $jumlah;
            Session::put('total_price', $total_price);
        } else if (Session::get('ordertype') == "Packaging") {
            if (Session::get('packaging_id') <> "") {
                $total_price = (Session::get('packaging_price') + $price_add_features) * Session::get('qty');
            } else {
                $total_price = 0;
            }
            Session::put('total_price', $total_price);
        } else if (Session::get('ordertype') == "Kaos") {
            if (Session::get('tshirt_detail_id') <> "") {
                $total_price = ((Session::get('tshirt_price') * (Session::get('qty') * 12)) + $price_add_features);
            } else {
                $total_price = 0;
            }
            Session::put('total_price', $total_price);
        } else if (Session::get('ordertype') == "Brosur") {
            if (Session::get('brosur_id') <> "") {
                $total_price = Session::get('brosur_price') * Session::get('qty');
            } else {
                $total_price = 0;
            }
            Session::put('total_price', $total_price);
        } else if (Session::get('ordertype') == "Nota") {
            if (Session::get('bill_id') <> "") {
                $total_price = Session::get('bill_price') * Session::get('qty');
            } else {
                $total_price = 0;
            }
            Session::put('total_price', $total_price);
        }



        return View::make('order.datapemesan')->with('total_price', $total_price);
    }

    public function PostDatapemesan() {
        $rules = array(
            'nama' => 'required',
            'email' => 'required|email',
            'jenis_kelamin' => 'required',
            'kontak' => 'required|numeric',
            'alamat' => 'required',
        );
        $notif = array(
            'required' => ' <font color="red">  Kolom harus diisi</font>',
            'email' => ' <font color="red">  Harap masukan email yang valid</font>',
            'numeric' => ' <font color="red">  Harap masukan kontak yang valid</font>',
        );

        $validator = Validator::make(Input::all(), $rules, $notif);
        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator)
                            ->withInput();
        }

        Session::put('nama', Input::get('nama'));
        Session::put('email', Input::get('email'));
        Session::put('alamat', Input::get('alamat'));
        Session::put('jenis_kelamin', Input::get('jenis_kelamin'));
        Session::put('kontak', Input::get('kontak'));

        $customer = New customer();
        $customer->customer_name = Session::get('nama');
        $customer->customer_gender = Session::get('jenis_kelamin');
        $customer->customer_address = Session::get('alamat');
        $customer->customer_phone = Session::get('kontak');
        $customer->customer_email = Session::get('email');
        $customer->save();


        if (Session::get('ordertype') == "ID Card") {
            if (Session::get('pilihan_jumlah') == "0") {
                $jumlah = 10;
            } else {
                $jumlah = Session::get('jumlah_idcard');
            }

            $order = New order();
            $order->order_customer_id = $customer->customer_id;
            $order->order_name = 'idcard';
            $order->order_qty = $jumlah;
            $order->order_qty_unit = 'Card';
            $order->order_total_price = Session::get('total_price');
            $order->order_desain = Session::get('design');
            $order->order_date = date('Y-m-d H:i:s');
            $order->save();

            $order = order::Find($order->order_id);
            $order->order_code = $order->order_id . $customer->customer_id . date('md');
            $order->save();

            $typename = refidcardtypes::Find(Session::get('jenis_idcard'));
            $qualityname = refidcardquality::Find(Session::get('kualitas_idcard'));
            $orderidcard = New orderidcard();
            $orderidcard->order_idcard_order_id = $order->order_id;
            $orderidcard->order_idcard_idcard_quality_id = Session::get('kualitas_idcard');
            $orderidcard->order_idcard_quality_name = $qualityname->idcard_quality_name;
            $orderidcard->order_idcard_idcard_type_id = Session::get('jenis_idcard');
            $orderidcard->order_idcard_type_name = $typename->idcard_type_name;
            $orderidcard->save();

            $ref = DB::table('ref_additional_features')
                    ->get();
            foreach ($ref as $ref) {
                if (Session::get($ref->additional_features_name) == '1') {

                    $orderidcarddetail = New orderidcarddetail();
                    $orderidcarddetail->order_idcard_detail_order_idcard_id = $orderidcard->order_idcard_id;
                    $orderidcarddetail->order_idcard_detail_idcard_additional_features_id = $ref->additional_features_id;
                    $orderidcarddetail->order_idcard_detail_feature_name = $ref->additional_features_name;
                    $orderidcarddetail->save();
                }
            }

            $invoice = New invoice();
            $invoice->invoice_order_id = $order->order_id;
            $invoice->invoice_total_payment = Session::get('total_price');
            $invoice->invoice_status = 'Belum lunas';
            $invoice->save();

            $progress = New progress;
            $progress->order_progress_order_id = $order->order_id;
            $progress->save();

            $result = DB::table('sys_order')
                    ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                    ->join('sys_order_idcard', 'order_idcard_order_id', '=', 'order_id')
                    ->where('order_id', '=', $order->order_id)
                    ->first();
            $ordername = 'Cetak ID Card';
        } else if (Session::get('ordertype') == "Packaging") {
            $order = New order();
            $order->order_customer_id = $customer->customer_id;
            $order->order_name = 'packaging';
            $order->order_qty = Session::get('qty');
            $order->order_qty_unit = 'Pcs';
            $order->order_total_price = Session::get('total_price');
            $order->order_desain = Session::get('design');
            $order->order_date = date('Y-m-d H:i:s');
            $order->save();

            $order = order::Find($order->order_id);
            $order->order_code = $order->order_id . $customer->customer_id . date('md');
            $order->save();

            $packaging = New orderpackaging;
            $packaging->order_packaging_order_id = $order->order_id;
            $packaging->order_packaging_packaging_id = Session::get('packaging_id');
            $packaging->order_packaging_type = Session::get('jenis_packaging');
            $packaging->order_packaging_size = Session::get('jenis_ukuran');
            $packaging->order_packaging_paper = Session::get('jenis_kertas');
            $packaging->save();

            $ref = DB::table('ref_additional_features')
                    ->get();
            foreach ($ref as $ref) {
                if (Session::get($ref->additional_features_name) == '1') {

                    $orderpackagingdetail = New orderpackagingdetail();
                    $orderpackagingdetail->order_packaging_detail_order_packaging_id = $packaging->order_packaging_id;
                    $orderpackagingdetail->order_packaging_detail_additional_features_id = $ref->additional_features_id;
                    $orderpackagingdetail->order_packaging_detail_additional_features_name = $ref->additional_features_name;
                    $orderpackagingdetail->save();
                }
            }

            $invoice = New invoice();
            $invoice->invoice_order_id = $order->order_id;
            $invoice->invoice_total_payment = Session::get('total_price');
            $invoice->invoice_status = 'Belum lunas';
            $invoice->save();

            $progress = New progress;
            $progress->order_progress_order_id = $order->order_id;
            $progress->save();

            $result = DB::table('sys_order')
                    ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                    ->join('sys_order_packaging', 'order_packaging_order_id', '=', 'order_id')
                    ->where('order_id', '=', $order->order_id)
                    ->first();
            $ordername = 'Cetak Packaging';
        } else if (Session::get('ordertype') == "Kaos") {
            $order = New order();
            $order->order_customer_id = $customer->customer_id;
            $order->order_name = 'tshirt';
            $order->order_qty = Session::get('qty');
            $order->order_qty_unit = 'Dozen';
            $order->order_total_price = Session::get('total_price');
            $order->order_desain = Session::get('design');
            $order->order_date = date('Y-m-d H:i:s');
            $order->save();

            $order = order::Find($order->order_id);
            $order->order_code = $order->order_id . $customer->customer_id . date('md');
            $order->save();

            $tshirt = New ordertshirt;
            $tshirt->order_tshirt_order_id = $order->order_id;
            $tshirt->order_tshirt_tshirt_id = Session::get('tshirt_detail_id');
            $tshirt->save();

            $ref = DB::table('ref_additional_features')
                    ->get();
            foreach ($ref as $ref) {
                if (Session::get($ref->additional_features_name) == '1') {

                    $ordertshirtdetail = New ordertshirtdetail();
                    $ordertshirtdetail->order_tshirt_detail_order_tshirt_id = $tshirt->order_tshirt_id;
                    $ordertshirtdetail->order_tshirt_detail_additional_features_id = $ref->additional_features_id;
                    $ordertshirtdetail->order_tshirt_detail_additional_features_name = $ref->additional_features_name;
                    $ordertshirtdetail->save();
                }
            }

            $invoice = New invoice();
            $invoice->invoice_order_id = $order->order_id;
            $invoice->invoice_total_payment = Session::get('total_price');
            $invoice->invoice_status = 'Belum lunas';
            $invoice->save();

            $progress = New progress;
            $progress->order_progress_order_id = $order->order_id;
            $progress->save();

            $result = DB::table('sys_order')
                    ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                    ->join('sys_order_tshirt', 'order_tshirt_order_id', '=', 'order_id')
                    ->where('order_id', '=', $order->order_id)
                    ->first();
            $ordername = 'Cetak T-Shirt';
        } else if (Session::get('ordertype') == "Nota") {
            $order = New order();
            $order->order_customer_id = $customer->customer_id;
            $order->order_name = 'nota';
            $order->order_qty = Session::get('qty');
            $order->order_qty_unit = 'Rim';
            $order->order_total_price = Session::get('total_price');
            $order->order_desain = Session::get('design');
            $order->order_date = date('Y-m-d H:i:s');
            $order->save();

            $order = order::Find($order->order_id);
            $order->order_code = $order->order_id . $customer->customer_id . date('md');
            $order->save();




            $billid = refbill::find(Session::get('bill_id'));


            $bill = New ordernota;
            $bill->order_bill_order_id = $order->order_id;

            $bill->order_bill_bill_id = Session::get('bill_id');
            $bill->order_bill_qty_ply = $billid->bill_qty_ply;
            $bill->order_bill_size = $billid->bill_size;
            $bill->order_bill_name = $billid->bill_name;
            $bill->order_bill_qty_color = Session::get('jumlah_warna');
            $paper = refpapertypes::find(Session::get('jenis_kertas'));
            $bill->order_bill_papertype = $paper->papertype_name;

            $bill->save();



            $invoice = New invoice();
            $invoice->invoice_order_id = $order->order_id;
            $invoice->invoice_total_payment = Session::get('total_price');
            $invoice->invoice_status = 'Belum lunas';
            $invoice->save();

            $progress = New progress;
            $progress->order_progress_order_id = $order->order_id;
            $progress->save();

            $result = DB::table('sys_order')
                    ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                    ->join('sys_order_bill', 'order_bill_order_id', '=', 'order_id')
                    ->where('order_id', '=', $order->order_id)
                    ->first();
            $ordername = 'Cetak Nota';
        } else if (Session::get('ordertype') == "Brosur") {
            $order = New order();
            $order->order_customer_id = $customer->customer_id;
            $order->order_name = 'brochure';
            $order->order_qty = Session::get('qty');
            $order->order_qty_unit = Session::get('brosur_unit');
            $order->order_total_price = Session::get('total_price');
            $order->order_desain = Session::get('design');
            $order->order_date = date('Y-m-d H:i:s');
            $order->save();

            $order = order::Find($order->order_id);
            $order->order_code = $order->order_id . $customer->customer_id . date('md');
            $order->save();



            //bill id <> 0
            if (Session::get('brosur_id') <> 0) {
                $brosurid = refbrosur::find(Session::get('brosur_id'));

                if ($brosurid->brochure_type == "brochure") {
                    $size = refbrochuresize::find(Session::get('brosur_size'));
                    $sizename = $size->size_brochure_name;
                } else if ($brosurid->brochure_type == "envelope") {
                    $size = refenvelopesize::find(Session::get('brosur_size'));
                    $sizename = $size->size_envelope_length . ' x ' . $size->size_envelope_width . ' ' . $size->size_envelope_unit;
                } else if ($brosurid->brochure_type == "flayer") {
                    $size = refflayersize::find(Session::get('brosur_size'));
                    $sizename = $size->size_flayer_length . ' x ' . $size->size_flayer_width . ' ' . $size->size_flayer_unit;
                } else if ($brosurid->brochure_type == "letterhead") {
                    $size = refletterheadsize::find(Session::get('brosur_size'));
                    $sizename = $size->size_letterhead_length . ' ' . $size->size_letterhead_width . ' ' . $size->size_letterhead_unit;
                }
            } else {
                if (Session::get('jenis_cetak') == "brochure") {
                    $size = refbrochuresize::find(Session::get('brosur_size'));
                    $sizename = $size->size_brochure_name;
                } else if (Session::get('jenis_cetak') == "envelope") {
                    $size = refenvelopesize::find(Session::get('brosur_size'));
                    $sizename = $size->size_envelope_length . ' x ' . $size->size_envelope_width . ' ' . $size->size_envelope_unit;
                } else if (Session::get('jenis_cetak') == "flayer") {
                    $size = refflayersize::find(Session::get('brosur_size'));
                    $sizename = $size->size_flayer_length . ' x ' . $size->size_flayer_width . ' ' . $size->size_flayer_unit;
                } else if (Session::get('jenis_cetak') == "letterhead") {
                    $size = refletterheadsize::find(Session::get('brosur_size'));
                    $sizename = $size->size_letterhead_length . ' ' . $size->size_letterhead_width . ' ' . $size->size_letterhead_unit;
                }
            }

            $brochure = New orderbrochure;
            $brochure->order_brochure_order_id = $order->order_id;

            $brochure->order_brochure_brochure_id = Session::get('brosur_id');
            $brochure->order_brochure_size = $sizename;

            $brochure->order_brochure_type = Session::get('jenis_cetak');
            $brochure->order_brochure_color = Session::get('warna');
            $brochure->order_brochure_qty_print_page = Session::get('qty_print_page');
            $paper = refpapertypes::find(Session::get('jenis_kertas'));
            $brochure->order_brochure_papertype = $paper->papertype_name;
            $brochure->save();






            $invoice = New invoice();
            $invoice->invoice_order_id = $order->order_id;
            $invoice->invoice_total_payment = Session::get('total_price');
            $invoice->invoice_status = 'Belum lunas';
            $invoice->save();

            $progress = New progress;
            $progress->order_progress_order_id = $order->order_id;
            $progress->save();

            $result = DB::table('sys_order')
                    ->join('sys_customer', 'customer_id', '=', 'order_customer_id')
                    ->join('sys_order_brochure', 'order_brochure_order_id', '=', 'order_id')
                    ->where('order_id', '=', $order->order_id)
                    ->first();
            $ordername = 'Cetak Brosur';
        }
        //send eail
        Mail::send('emails.terimakasih', array('pesan' => 'Terima kasih atas pesanan anda.', 'ordercode' => $order->order_code), function($message) {
            $email = Input::get('email');
            $nama = Input::get('nama');
            $message->to($email, $nama)->subject('Terima kasih');
        });

        Session::keep(array('username'));
        return View::make('order.orderresult', compact('ordername', $ordername, 'result', $result));
    }

    public function getOrderresult() {
        return View::make('order.orderresult');
    }

}
